using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using WhittleList.Bll;
using WhittleList.Helpers;

namespace WhittleList.Controllers
{
    [Authorize]
    public class SectionController : BaseController
    {
        public SectionController()
            : this(new UserRepository(), new SiteRepository())
        {
        }

        public SectionController(ISiteRepository siteRepository)
            : this(new UserRepository(), siteRepository)
        {
        }
        
        public SectionController(IUserRepository userRepository)
            : this(userRepository, new SiteRepository())
        {
        }

        public SectionController(IUserRepository userRepository, ISiteRepository siteRepository)
            : base(userRepository, siteRepository)
        {
        }
        //
        // GET: /Section/

        public ActionResult Index(int projectId)
        {
            var data = SiteRepository.Section_GetAllByProjectId(projectId).ToList();
            return View(data);
        }

        //
        // GET: /Section/Details/5

        public ActionResult Details(Site site, int id)
        {
            var data = SiteRepository.Section_GetById(id);
            if (site != null && data != null && data.Project.SiteId == site.Id)
                return View(data);
            else
                throw new NotImplementedException();
        }

        //
        // GET: /Section/Create

        public ActionResult Create(Site site, int projectId)
        {
            var project = SiteRepository.Project_GetById(projectId);
            if (site != null && project != null && project.SiteId == site.Id)
            {
                return View();
            }
            else
            {
                throw new NotImplementedException();
            }
        } 

        //
        // POST: /Section/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Site site, int projectId, string name)
        {
            var project = SiteRepository.Project_GetById(projectId);
            if(site != null && project != null && project.SiteId == site.Id)
            {
                var section = new Section();
                section.ProjectId = projectId;
                section.Name = name;

                try
                {
                    SiteRepository.Insert(section);
                    SiteRepository.SaveChanges(CurrentUser);
                    return RedirectToAction("Details", "Project", new { id = projectId });
                }
                catch(WhittleList.Bll.Exceptions.DuplicateNameException)
                {
                    this.ModelState.AddModelError("Name", string.Format("The name '{0}' already exists for a section in this project", name));
                }
                catch (WhittleList.Bll.Exceptions.ValidationException e)
                {
                    this.ModelState.AddModelErrors(e.RuleViolations);
                }
                return View(section);
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        //
        // GET: /Section/Edit/5
 
        public ActionResult Edit(Site site, int id)
        {
            var section = SiteRepository.Section_GetById(id);
            if (site != null && section != null && section.Project.SiteId == site.Id)
            {
                return View(section);
            }
            else
                throw new NotImplementedException();
        }

        //
        // POST: /Section/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Site site, int id, string name, System.Data.Linq.Binary cid)
        {
            var section = SiteRepository.Section_GetById(id);
            if (site != null && section != null && section.Project.SiteId == site.Id && section.CheckConcurrent(cid))
            {
                section.Name = name;
                try
                {
                    SiteRepository.SaveChanges(CurrentUser);
                    return RedirectToAction("Index");
                }
                catch (WhittleList.Bll.Exceptions.DuplicateNameException)
                {
                    ModelState.AddModelError("Name", string.Format("A Section called '{0}' already exists for this project", name));
                }
                catch (WhittleList.Bll.Exceptions.ValidationException e)
                {
                    ModelState.AddModelErrors(e.RuleViolations);
                }
                return View(section);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
    }
}
